/**
 * 格式化日期
 * @param {Date|Number} d 日期 
 * @param {String} fmt 格式化
 * @return {String} YYYY-MM-DD hh:mm:ss
 */
export const format = (d, fmt='YYYY-MM-DD hh:mm:ss') => {
    const date = d ? new Date(d) : new Date()
    const o = {
        "Y+": date.getFullYear(),
        "M+": date.getMonth() + 1,     // 月
        "D+": date.getDate(),          // 日
        "h+": date.getHours(),         // 时
        "m+": date.getMinutes(),       // 分
        "s+": date.getSeconds(),       // 秒
        "W": date.getDay()             // 周
    };

    for (var k in o) {
       if (new RegExp("("+ k +")").test(fmt)) {
            fmt = fmt.replace(RegExp.$1, () => {
                if (k === 'W') {                                    // 星期几
                    const week = ['日', '一', '二', '三', '四', '五', '六']
                    return week[o[k]]
                } else if (k === 'Y+' || RegExp.$1.length == 1) {    // 年份 or 小于10不加0
                    return o[k]
                } else {
                    return ("00"+ o[k]).substr(("" + o[k]).length)  // 小于10补位0
                }
            })
        }
    }
    return fmt
}

/**
 * @param {number} time
 * @param {string} option
 * @returns {string}
 */
export const formatTime = (time, option) => {
    if (("" + time).length === 10) {
        time = parseInt(time) * 1000;
    } else {
        time = +time;
    }
    const d = new Date(time);
    const now = Date.now();

    const diff = (now - d) / 1000;

    if (diff < 30) {
        return "刚刚";
    } else if (diff < 3600) {
        // less 1 hour
        return Math.ceil(diff / 60) + "分钟前";
    } else if (diff < 3600 * 24) {
        return Math.ceil(diff / 3600) + "小时前";
    } else if (diff < 3600 * 24 * 2) {
        return "1天前";
    }
    if (option) {
        return parseTime(time, option);
    } else {
        return (
            d.getMonth() +
            1 +
            "月" +
            d.getDate() +
            "日" +
            d.getHours() +
            "时" +
            d.getMinutes() +
            "分"
        );
    }
}

/**
 * 获取某一天的 00:00:00 和 23:59:59
 * @param {Date} date 日期
 * @return {object}
 */
export const getDayStartEnd = (date) => {
    const timestamp = new Date(date).getTime()
    const start = new Date(new Date(timestamp).setHours(0, 0, 0, 0))
    const end = new Date(new Date(timestamp).setHours(23, 59, 59, 999))
    return {
      start,
      end
    }
  }

/**
 * 获取今天0点时间戳-秒
 * @return {number}
 */
export const getTodayTimeStamp = () =>
    new Date(new Date().setHours(0, 0, 0, 0)) / 1000;

/**
 * 获取当天0点时间戳-毫秒
 * @return {number}
 */
export const getTodayTimestamp = () => new Date().setHours(0, 0, 0, 0);

/**
 * 获取当天前x天的时间戳
 * @return
 */
export const getBeforeDay = day => new Date().setHours(0, 0, 0, 0) - 86400000 * day;

/**
 * 获取当天后x天的时间戳
 * @return
 */
export const getAfterDay = day => new Date().setHours(0, 0, 0, 0) + 86400000 * day;

/**
 * 毫秒转化成天时分秒的时间格式
 */
export const formatDuring = mss => {
    let days = parseInt(mss / (1000 * 60 * 60 * 24));
    let hours = parseInt((mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    let minutes = parseInt((mss % (1000 * 60 * 60)) / (1000 * 60));
    let seconds = (mss % (1000 * 60)) / 1000;
    return (
        days + " 天 " + hours + " 小时 " + minutes + " 分钟 " + seconds + " 秒 "
    );
}

/**
 * 格式化星期
 * @param weekNubmer
 */
export const formatWeek = weekNubmer => {
    const weeks = ["日", "一", "二", "三", "四", "五", "六"];
    return weeks[weekNubmer] || "未知";
}

/**
 * 当前时区
 */
export const timeZone = () => {
    const now = new Date();
    const times = now.getHours();
    let greetingsWord = null;
    if (times >= 0 && times < 6) {
        greetingsWord = '凌晨';
    } else if (times >= 6 && times < 8) {
        greetingsWord = '早上';
    } else if (times >= 8 && times < 9) {
        greetingsWord = '上午';
    } else if (times >= 11 && times < 13) {
        greetingsWord = '中午';
    } else if (times >= 13 && times < 17) {
        greetingsWord = '下午';
    } else if (times >= 17 && times < 19) {
        greetingsWord = '傍晚';
    } else if (times >= 19 && times < 24) {
        greetingsWord = '晚上';
    }
    return greetingsWord;
}

/**
 * 日期范围相关方法
 * 获取本周、本月、上月、本季度的开始日期、结束日期
 */
export const dateRange = () => {
    const now = new Date() // 当前日期
    const nowDayOfWeek = now.getDay() // 今天本周的第几天
    const nowDay = now.getDate() // 当前日
    const nowMonth = now.getMonth() // 当前月
    let nowYear = now.getFullYear() // 当前年
    nowYear += nowYear < 2000 ? 1900 : 0
  
    const lastMonthDate = new Date() // 上月日期
    lastMonthDate.setDate(1)
    lastMonthDate.setMonth(lastMonthDate.getMonth() - 1)
    const lastMonth = lastMonthDate.getMonth()
  
    // 获得某月的天数
    function getMonthDays (month) {
      const monthStartDate = new Date(nowYear, month, 1)
      const monthEndDate = new Date(nowYear, month + 1, 1)
      const days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24)
      return days
    }
  
    // 获得本季度的开始月份
    function getQuarterStartMonth () {
      let quarterStartMonth = 0
      if (nowMonth < 3) {
        quarterStartMonth = 0
      }
      if (nowMonth > 2 && nowMonth < 6) {
        quarterStartMonth = 3
      }
      if (nowMonth > 5 && nowMonth < 9) {
        quarterStartMonth = 6
      }
      if (nowMonth > 8) {
        quarterStartMonth = 9
      }
      return quarterStartMonth
    }
  
    // 获得本周的开始日期
    function getWeekStartDate () {
      const weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek + 1)
      return weekStartDate
    }
  
    // 获得本周的结束日期
    function getWeekEndDate () {
      const weekEndDate = new Date(nowYear, nowMonth, nowDay + (7 - nowDayOfWeek))
      return weekEndDate
    }
  
    // 获得本月的开始日期
    function getMonthStartDate () {
      const monthStartDate = new Date(nowYear, nowMonth, 1)
      return monthStartDate
    }
  
    // 获得本月的结束日期
    function getMonthEndDate () {
      const monthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowMonth))
      return monthEndDate
    }
  
    // 获得上月开始时间
    function getLastMonthStartDate () {
      const lastMonthStartDate = new Date(nowYear, lastMonth, 1)
      return lastMonthStartDate
    }
  
    // 获得上月结束时间
    function getLastMonthEndDate () {
      const lastMonthEndDate = new Date(nowYear, lastMonth, getMonthDays(lastMonth))
      return lastMonthEndDate
    }
  
    // 获得本季度的开始日期
    function getQuarterStartDate () {
      const quarterStartDate = new Date(nowYear, getQuarterStartMonth(), 1)
      return quarterStartDate
    }
  
    // 获得本季度的结束日期
    function getQuarterEndDate () {
      const quarterEndMonth = getQuarterStartMonth() + 2
      const quarterStartDate = new Date(nowYear, quarterEndMonth, getMonthDays(quarterEndMonth))
      return quarterStartDate
    }
  
    // 获取本年开始
    function getYearStartDate () {
      const yearStartDate = new Date(nowYear, 0, 1)
      return yearStartDate
    }
  
    // 获取本年开始
    function getYearEndDate () {
      const yearEndDate = new Date(nowYear + 1, 0, 0).setHours(23, 59, 59, 999)
      return yearEndDate
    }
  
    return {
      now,
      getWeekStartDate,
      getWeekEndDate,
      getMonthStartDate,
      getMonthEndDate,
      getLastMonthStartDate,
      getLastMonthEndDate,
      getQuarterStartDate,
      getQuarterEndDate,
      getYearStartDate,
      getYearEndDate
    }
  }